import { _decorator, Component, Node, find, Input, Script, sys } from 'cc';
import audioController from '../audio/audioController'
import { AudioManager } from "../audio/AudioManager";
import useStore from "../store";
import api from '../api'
const { ccclass, property } = _decorator;

@ccclass('homeInit')
export class homeInit extends Component {

    @property(Script)
    audioController: Script = null

    onLoad() {
        console.log('onLoad')
    }

    start() {
        console.log('start')
        this.bootstrapApi()
        this.handleMusic()
    }
    

    // 初始化
    bootstrapApi() {
        if (useStore().userStore.token) {
            api.user.getReaderInfo()
        } else {
            this.createUserInfoButton()
        }

    }

    // 增加登录按钮
    createUserInfoButton() {
        let sysInfo = wx.getSystemInfoSync();
        // 获取微信界面大小
        let screenWidth = sysInfo.screenWidth;
        let screenHeight = sysInfo.screenHeight;
        const button = wx.createUserInfoButton(
            {
                type: 'text',
                text: '',
                style: {
                    left: 0,
                    top: 0,
                    width: screenWidth,
                    height: screenHeight,
                    lineHeight: 40,
                    backgroundColor: '#00000000',
                    color: '#ffffff',
                    textAlign: 'center'
                }
            }
        )
        button.onTap((res) => {
            console.log(res, 'createUserInfoButton')
            if (res.errMsg === 'getUserInfo:ok') { 
                api.user.getUserProfile(res.userInfo)
            }
        })
    }

    handleMusic () {
        if (AudioManager.musicVolume === 1) {
            AudioManager.playMusic()
        }
    }

    update(deltaTime: number) {

    }
}

